package com.lin.manager.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.manager.common.enums.LogEnum;
import com.lin.manager.common.resp.DataPage;
import com.lin.manager.system.dao.SysOperateLogDao;
import com.lin.manager.system.entity.SysOperateLogEntity;
import com.lin.manager.system.model.vo.LogQueryParamVO;
import com.lin.manager.system.model.vo.OperateLogVO;
import com.lin.manager.system.service.SysOperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @作者: 林江
 * @创建时间: 2024-03-19 14:51:14
 * @功能:
 */
@Slf4j
@Service
public class SysOperateLogServiceImpl implements SysOperateLogService {

    @Resource
    private SysOperateLogDao sysOperateLogDao;


    @Override
    public void addLog(OperateLogVO operateLogVO) {
        SysOperateLogEntity operateLog = new SysOperateLogEntity();
        operateLog.setDescript(operateLogVO.getDescript());
        operateLog.setType(operateLogVO.getType());
        operateLog.setIp(operateLogVO.getIp());
        operateLog.setModule(operateLogVO.getModule());
        operateLog.setCreateTime(operateLogVO.getCreateTime());
        operateLog.setRespParams(operateLogVO.getRespParams());
        operateLog.setReqParams(operateLogVO.getReqParams());
        operateLog.setReqMethod(operateLogVO.getMethod());
        operateLog.setReqUri(operateLogVO.getReqUri());
        operateLog.setCMethod(operateLogVO.getcMethod());
        operateLog.setConsumerTime(operateLogVO.getConsumerTime());
        operateLog.setUserName(operateLogVO.getUserName());
        sysOperateLogDao.insert(operateLog);
    }

    @Override
    public void delLog(Object id) {
        sysOperateLogDao.deleteById(id.toString());
    }

    @Override
    public DataPage<SysOperateLogEntity> listLogOperateByPage(LogQueryParamVO pageVO) {
        LambdaQueryWrapper<SysOperateLogEntity> wrapper = new QueryWrapper<SysOperateLogEntity>().lambda()
                .orderByDesc(SysOperateLogEntity::getCreateTime)
                .and(StringUtils.hasText(pageVO.getKey()), w -> w.like(SysOperateLogEntity::getDescript, pageVO.getKey()).or().like(SysOperateLogEntity::getModule, pageVO.getKey()))
                .ge(StringUtils.hasText(pageVO.getStartTime()), SysOperateLogEntity::getCreateTime, pageVO.getStartTime())
                .le(StringUtils.hasText(pageVO.getEndTime()), SysOperateLogEntity::getCreateTime, pageVO.getEndTime())
                .eq(pageVO.getMark().equals(NumberUtils.INTEGER_ONE), SysOperateLogEntity::getType, LogEnum.SELECT)
                .ne(pageVO.getMark().equals(NumberUtils.INTEGER_TWO), SysOperateLogEntity::getType, LogEnum.SELECT);

        Page<SysOperateLogEntity> selectPage = sysOperateLogDao.selectPage(new Page<>(pageVO.getPageNum(), pageVO.getPageSize()), wrapper);
        return DataPage.<SysOperateLogEntity>builder().page(selectPage).build();
    }

//    @Override
//    public List<LogVo> statistics(Integer type) {
//        Map<String, String> dateMap = DateTimeUtil.getFirstAndLast(type);
//        List<SysOperateLogEntity> data = sysOperateLogDao.selectList(new LambdaQueryWrapper<SysOperateLogEntity>().between(
//                SysOperateLogEntity::getCreateTime,
//                dateMap.get(DateTimeUtil.START_TIME),
//                dateMap.get(DateTimeUtil.END_TIME)
//        ));
//
//        // 获取中间时间
//        List<String> middleDateTimes = DateTimeUtil.getMiddleDateTime(dateMap.get(DateTimeUtil.START_TIME), dateMap.get(DateTimeUtil.END_TIME), type);
//
//
//        List<LogVo> result = new ArrayList<>();
//        for (String middleDateTime : middleDateTimes) {
//            long num = data.stream().filter(item -> DateTimeUtil.convertDateToStr(item.getCreateTime()).startsWith(middleDateTime)).count();
//
//            LogVo logVo = new LogVo();
//            logVo.setKey(middleDateTime);
//            logVo.setValue(num);
//            result.add(logVo);
//        }
//        return result;
//    }

}
